package com.fang.shirotestnoredis.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class OSSUtil {

    // //真实文件名称
    // private String realFileName;
    //
    // //上传的文件名
    // private String fileName;
    //
    // public String getFileName(){
    //     return fileName;
    // }
    //
    // //文件大小
    // private String fileSize;
    //
    // public String getRealFileName(){
    //     return this.realFileName;
    // }
    //
    // public String getFileSize(){
    //     return this.fileSize;
    // }
    //
    // /**
    //  * 把文件保存在阿里云OSS,返回路径保存到数据库
    //  */
    // public String getFileUrl(MultipartFile file) throws IOException {
    //     //创建OSSClient实例,参数：连接到阿里云OSS对象存储的信息
    //     OSSClient ossClient = new OSSClient(WebFinalVariable.ENDPOINT,WebFinalVariable.ACCESSKEY_ID,WebFinalVariable.ACCESSKEY_SECRET);
    //     //文件名格式
    //     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
    //     this.fileName = file.getOriginalFilename();
    //     //设置真实文件名
    //     this.realFileName = simpleDateFormat.format(new Date()) + file.getOriginalFilename();
    //     //设置文件大小
    //     this.fileSize = Math.ceil(file.getSize()/1024.0) + "kb";
    //     //文件上传
    //     ossClient.putObject(WebFinalVariable.BUCKET_NAME,this.realFileName,new ByteArrayInputStream(file.getBytes()));
    //     //设置URL过期时间为永久
    //     Date expiration = new Date(new Date().getTime() + Integer.MAX_VALUE);
    //     //生成URL
    //     URL url = ossClient.generatePresignedUrl(WebFinalVariable.BUCKET_NAME,this.realFileName,expiration);
    //     ossClient.shutdown();
    //     return url.toString();
    // }
    //
    // /**
    //  * 文件下载
    //  */
    // public void downloadFile(String realFileName, HttpServletResponse response) throws IOException {
    //     //创建OSSClient实例,参数：连接到阿里云OSS对象存储的信息
    //     OSSClient ossClient = new OSSClient(WebFinalVariable.ENDPOINT,WebFinalVariable.ACCESSKEY_ID,WebFinalVariable.ACCESSKEY_SECRET);
    //     OSSObject ossObject = ossClient.getObject(WebFinalVariable.BUCKET_NAME,realFileName);
    //     //获取ossObject中的输入流，读取文件内容
    //     BufferedInputStream bufferedInputStream = new BufferedInputStream(ossObject.getObjectContent());
    //     BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
    //     //通知浏览器以附件形式下载
    //     response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(realFileName,"utf-8"));
    //     byte[] bytes = new byte[4096];
    //     int length = 0;
    //     while ((length = bufferedInputStream.read(bytes)) != -1){
    //         outputStream.write(bytes,0,length);
    //     }
    //     if (outputStream != null){
    //         outputStream.flush();
    //         outputStream.close();
    //     }
    //     if (bufferedInputStream != null){
    //         bufferedInputStream.close();
    //     }
    //     ossClient.shutdown();
    // }
    //
    // /**
    //  * 删除文件
    //  */
    // public void deleteFile(String realFileName){
    //     //创建OSSClient实例,参数：连接到阿里云OSS对象存储的信息
    //     OSSClient ossClient = new OSSClient(WebFinalVariable.ENDPOINT,WebFinalVariable.ACCESSKEY_ID,WebFinalVariable.ACCESSKEY_SECRET);
    //     ossClient.deleteObject(WebFinalVariable.BUCKET_NAME,realFileName);
    //     ossClient.shutdown();
    // }

}
